package com.example.accessingdatajpa.controller;

import com.example.accessingdatajpa.pojo.Customer;
import com.example.accessingdatajpa.pojo.ShoppingCart;
import com.example.accessingdatajpa.response.RestResponse;
import com.example.accessingdatajpa.service.IShoppingCartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.parameters.P;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@RestController
@RequestMapping("/app")
public class ShoppingCartController {
    @Autowired
    final private IShoppingCartService shoppingCartService;

    public ShoppingCartController(IShoppingCartService shoppingCartService) {
        this.shoppingCartService = shoppingCartService;
    }

    //find all goods in shopping cart by customer id
    @GetMapping("/allInShoppingCart/{customerId}")
    public @ResponseBody RestResponse<List<ShoppingCart>> findAllShoppingCart(@PathVariable("customerId") Long customerId){
        return shoppingCartService.queryAllInShoppingCartById(customerId);
    }

    //find all goods in order management or shopping cart
    @GetMapping("/allInOM")
    public @ResponseBody RestResponse<List<ShoppingCart>> findAllInOM(@RequestParam Long customerId,@RequestParam Boolean isSubmit){
        return shoppingCartService.queryAllGoodsInOM(customerId,isSubmit);
    }

    //find all goods in order management or shopping cart(cookie实验版本)
    @GetMapping("/allGoodsInOM")
    public @ResponseBody RestResponse<List<ShoppingCart>> findInOM(@RequestParam Long customerId,@RequestParam Boolean isSubmit,HttpServletRequest request,HttpServletResponse rep){
        return shoppingCartService.queryGoodsInOM(customerId,isSubmit,request,rep);
    }

    //find goods in order management
    @GetMapping("/goodsInOM")
    public @ResponseBody RestResponse<List<ShoppingCart>> findGoodsInOM(@RequestParam Long customerId,@RequestParam Integer orderManagement){
        return shoppingCartService.queryGoodsInOM(customerId, orderManagement);
    }

    //add goods to shopping cart
    @PostMapping("/addGoodsToCart/{customerId}")
    public @ResponseBody RestResponse<String> addGoodsToShoppingCart(@RequestBody ShoppingCart shoppingCart,@PathVariable("customerId") Long customerId){
        return shoppingCartService.addInShoppingCart(shoppingCart,customerId);
    }

    //add goods to shopping cart(cookie实验版本)
    @PostMapping("/addGoodsToShoppingCart/{customerId}")
    public @ResponseBody RestResponse<String> addGoodsToShoppingCart(@RequestBody ShoppingCart shoppingCart, @PathVariable("customerId") Long customerId, HttpServletRequest request, HttpServletResponse rep){
        return shoppingCartService.addShoppingCart(shoppingCart,customerId,request,rep);
    }

    //update the account of goods and re-calculate the total value
    @PostMapping("/updateAmount/{shoppingId}/{goodsAmount}")
    public @ResponseBody RestResponse<String> updateAmount(@PathVariable("shoppingId") Long shoppingId,@PathVariable("goodsAmount") Long goodsAmount){
        return shoppingCartService.updateAccountInCartById(shoppingId,goodsAmount);
    }

    //select the goods or not
    @PostMapping("/select/{shoppingId}/{goodsSelect}")
    public @ResponseBody RestResponse<String> selectGoods(@PathVariable("shoppingId") Long shoppingId,@PathVariable("goodsSelect") Boolean goodsSelect){
        return shoppingCartService.chooseGoods(shoppingId, goodsSelect);
    }

    //manage order
    @PostMapping("/orderManagement/{shoppingId}")
    public @ResponseBody RestResponse<String> orderManage(@PathVariable("shoppingId") Long shoppingId,Boolean isSubmit,Integer orderManagement){
        return shoppingCartService.manageOrder(shoppingId, isSubmit, orderManagement);
    }

    //delete by id
    @DeleteMapping("/deleteCart/{shoppingId}")
    public @ResponseBody RestResponse<String> deleteCart(@PathVariable("shoppingId") Long shoppingId){
        return shoppingCartService.deleteGoods(shoppingId);
    }
}
